﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using CG.Security;

namespace td.Core.Security
{
    public partial class RoleManagerForm : DevExpress.XtraEditors.XtraUserControl,IToolButton
    {
        public RoleManagerForm()
        {
            InitializeComponent();
            WorkBenchSingleTon.WorkBench.ActivateTabPageChanged += new EventHandler(WorkBench_ActivateTabPageChanged);
            this.Disposed += new EventHandler(RoleManagerForm_Disposed);
            
        }

        void RoleManagerForm_Disposed(object sender, EventArgs e)
        {
            WorkBenchSingleTon.WorkBench.ActivateTabPageChanged -= new EventHandler(WorkBench_ActivateTabPageChanged);
        }

        void WorkBench_ActivateTabPageChanged(object sender, EventArgs e)
        {
             var p = WorkBenchSingleTon.WorkBench.GetActivteTabPage();
            if (p != null)
            {
                if (p.Title == "角色管理")
                {
                    SetComBoxDataSource();
                }
            }
        }
        DataTable m_roleTable, m_rightTable, m_roleRightTable;
        public DataTable RoleTable
        {
            get { return this.m_roleTable; }
            set
            {
                this.m_roleTable = value;
                SetComBoxDataSource();
            }
        }
        /// <summary>
        /// 为角色添加权限  >
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton11_Click(object sender, EventArgs e)
        {
            try
            {

                // Get the current right index .
                int rightIndex = m_listBoxRights2.SelectedIndex;

                // Should we ignore the the event?
                if (rightIndex == -1)
                    return;

                // Get the current role index.
                int roleIndex = m_comboBoxRoles.ItemIndex;

                // Should we ignore the the event?
                if (roleIndex == -1)
                    return;

                // Get the identifiers.
                int roleID = (Int32)m_roleTable.Rows[roleIndex]["role_id"];
                int rightID = (Int32)m_rightTable.Rows[rightIndex]["right_id"];

                // Search for an existing row.
                DataRow[] rows = m_roleRightTable.Select(
                    "right_id = " + rightID + " AND role_id = " + roleID
                    );

                // Is there already an association?
                if (rows.Length > 0)
                    return;

                // Create a new association.
                RoleRightManager.Create(roleID, rightID);

                // Get the rights for the role.
                m_roleRightTable = RoleRightManager.FindByRole(roleID).Tables[0];

                // Bind the GUI to the table.
                m_listBoxRoleRights.DataSource = m_roleRightTable;
                m_listBoxRoleRights.DisplayMember = "right_name";


            } // End try

            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            } // End catch

        }

        private void RoleManagerForm_Load(object sender, EventArgs e)
        {           
            SetComBoxDataSource();
        }

        public void SetComBoxDataSource()
        {
            m_roleTable = RoleManager.FindAll().Tables[0];
            this.m_rightTable = RightManager.FindAll().Tables[0];
            this.m_listBoxRights2.DataSource = m_rightTable;
            this.m_listBoxRights2.DisplayMember = "right_name";
            this.m_listBoxRights2.SelectedIndex = 0;
            m_comboBoxRoles.Properties.DataSource = m_roleTable;
            m_comboBoxRoles.Properties.DisplayMember = "role_name";
            if (m_comboBoxRoles.Properties.Columns.Count == 3)
            {
                m_comboBoxRoles.Properties.Columns[0].Caption = "编号";
                m_comboBoxRoles.Properties.Columns[1].Caption = "角色名";
                m_comboBoxRoles.Properties.Columns[2].Caption = "角色描述";
            }
            m_comboBoxRoles.ItemIndex = 0;
        }
        /// <summary>
        /// 删除角色权限 <
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void simpleButton10_Click(object sender, EventArgs e)
        {
            try
            {

                // Get the current right index.
                int rightIndex = m_listBoxRoleRights.SelectedIndex;

                // Should we ignore the the event?
                if (rightIndex == -1)
                    return;

                // Get the current role index.
                int roleIndex = m_comboBoxRoles.ItemIndex;

                // Should we ignore the the event?
                if (roleIndex == -1)
                    return;

                // Get the identifiers.
                int roleID = (Int32)m_roleTable.Rows[roleIndex]["role_id"];
                int rightID = (Int32)m_roleRightTable.Rows[rightIndex]["right_id"];

                // Delete the association.
                RoleRightManager.Delete(roleID, rightID);

                // Get the rights for the role.
                m_roleRightTable = RoleRightManager.FindByRole(roleID).Tables[0];

                // Bind the GUI to the table.
                m_listBoxRoleRights.DataSource = m_roleRightTable;
                m_listBoxRoleRights.DisplayMember = "right_name";


            } // End try

            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
            } // End catch

        }


        private void m_comboBoxRoles_EditValueChanged(object sender, EventArgs e)
        {
             
      


            // Get the current role index.
            int index = m_comboBoxRoles.ItemIndex;

            // Should we ignore the the event?
            if (index == -1)
                return;

            // Get the identifier for the role.
            int roleID = (Int32)m_roleTable.Rows[index]["role_id"];

            // Get the rights for the selected role.
            m_roleRightTable = RoleRightManager.FindByRole(roleID).Tables[0];

            // Bind the GUI.
            m_listBoxRoleRights.DataSource = m_roleRightTable;
            m_listBoxRoleRights.DisplayMember = "right_name";
     

        }

        public void OnNew()
        {
            AddRole addrole = new AddRole();
            if (addrole.ShowDialog() == DialogResult.OK)
            {
                CG.Security.RoleManager.Create(addrole.RoleName, addrole.RoleDescription);
                this.SetComBoxDataSource();
            }
        }

        public void OnSave()
        {
            
        }

        public void OnSaveAs()
        {
           
        }

        public void OnDelete()
        {
            
        }
    }
}
